<template>
<el-dialog width="70%" title="添加/编辑服务" :visible.sync="petPublishDialog" :show-close="false" :destroy-on-close="true" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="petPublish" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        
        <el-form-item label="发布编号" prop="serviceTypeName">
          <el-input v-model="petPublish.publishId"></el-input>
        </el-form-item>
        <el-form-item label="发布人" prop="userName">
          <el-input v-model="petPublish.userName" ></el-input>
        </el-form-item>
         <el-form-item label="分类" prop="userName">
          <el-input v-model="petPublish.typeName" ></el-input>
        </el-form-item>
        <el-form-item label="发布标题" prop="userName">
          <el-input v-model="petPublish.publishTitle" ></el-input>
        </el-form-item>
        <el-form-item label="发布内容" prop="userName">
         <Tinymce @input="changeHandler" :value="petPublish.publishContent"> </Tinymce>
          <!-- <el-input v-model="petPublish.publishContent"></el-input> -->
        </el-form-item>
       <el-form-item label="发布路径" prop="userName">
          <!-- <el-input v-model="petPublish.publishPath" ></el-input> -->
           <QiniuUploadMultiple :fileListPre="petPublish.publishPath" @changeImage="changeIcons"></QiniuUploadMultiple>
        </el-form-item>
        <el-form-item label="发布类型" prop="type" >
          <el-select v-model="petPublish.publishType" placeholder="发布类型" >
            <el-option label="图片" :value="1" key="1"></el-option>
            <el-option label="视频" :value="2" key="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布位置名称" prop="userName">
          <el-input v-model="petPublish.positionName" ></el-input>
        </el-form-item>
        <el-form-item label="发布位置的具体地址" prop="userName">
          <el-input v-model="petPublish.positionAddr" ></el-input>
        </el-form-item>
        <el-form-item label="发布时间" prop="userName" class="form-item-time">
          <el-date-picker
            v-model="petPublish.publishTime"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发布状态" prop="type" >
          <el-select v-model="petPublish.publishStatus" placeholder="发布类型" >
            <el-option label="草稿" :value="0" key="0"></el-option>
            <el-option label="提交待审核" :value="1" key="1"></el-option>
            <el-option label="审核不通过" :value="2" key="2"></el-option>
            <el-option label="审核通过" :value="3" key="3"></el-option>
            <el-option label="禁播" :value="4" key="4"></el-option>
          </el-select>
        </el-form-item>
        
      </el-form>
      <div slot="footer">
        <el-button @click="closeHandler()">取消</el-button>
        <el-button type="success" @click="verify(3)">通过</el-button>
        <el-button type="info" @click="verify(2)">不通过</el-button>
        <el-button type="danger" @click="verify(4)" >禁播</el-button>
      </div>
</el-dialog>
   
</template>

<script>
import {replaceUrlParam,petPublishSaveUrl,userByIdUrl,petTypeByIdUrl} from '@/utils/common'
import Tinymce from '../tinymce/tinymce-edit-vue'
import QiniuUploadMultiple from '../qiniu/upload-multiple'

export default {
  props: {
    petPublishDialog: {
      type: Boolean,
      default: false
    },
    popPetPublish: {
      type: Object,
      default: null
    }
  },
  watch: {
   popPetPublish() {
      this.petPublish = this.popPetPublish; 
    }
  },
  components:{Tinymce,QiniuUploadMultiple},
  data() {
    
    return {
      loading: false,
      petPublish: {
      },
      userList:[],
      typeList:[]
    }
  },
  mounted() {


  },
  methods: {
    loadUser( userId){ //加载单个用户
      const _self = this;
      this.axios.post(replaceUrlParam(userByIdUrl,[userId])).then(res=>{
        if(res.data.status=="SUCCESS"&&res.data.data){
          _self.petPublish.userName=res.data.data.userName
        }
      
      })
    },
    loadPetType(typeId){
      const _self = this;
      this.axios.post(replaceUrlParam(petTypeByIdUrl,[typeId])).then(res=>{
        if(res.data.status=="SUCCESS"&&res.data.data){
          _self.petPublish.typeName=res.data.data.typeName
        }
      
      })
    },
    verify(publishStatus){
      this.petPublish.publishStatus=publishStatus;
      this.submitForm("ruleForm")
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.saveService();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    saveService() {
      this.loading = true;
      this.axios
        .post(petPublishSaveUrl, this.petPublish)
        .then(res => {
          if (res.data.status == "SUCCESS") {
            this.loading = false;
            this.$emit("close");
            this.$message.success(res.data.result);
          } else {
            this.loading = false;
            this.$message.error(res.data.result);
          }
        })
        .catch(response => {
          this.loading = false;
          console.log("response - " + response);
          this.$message.error("数据保存异常失败");
        });
    },
    changeHandler(value){
      this.petPublish.publishContent=value;
    },
    closeHandler: function () {
      this.$emit("close");
    },
    changeIcons(image){
      this.petPublish.publishPath= image
      this.indoorPhoto="";
    },
  }
};
</script>

<style lang="less" scoped>
.form-item-time{
  /deep/.el-form-item__content{
    text-align: left;
  }
}
</style>
